<html>
<body>
<table border="1" width="300">
<tr><td width="100">文件大小</td><td width="200"><div id="filesize">未知长度</div></td></tr>
<tr><td>已经下载</td><td><div id="downloaded">0</div></td></tr>
<tr><td>完成进度</td><td><div id="progressbar" style="float:left;width:0px;text-align:center;color:#FFFFFF;background-color:blue"></div><div id="progressText" style=" float:left">0%</div></td></tr>
</table>
<script type="text/javascript">
//文件长度
var filesize=0;
function $(obj) {return document.getElementById(obj);}

//设置文件长度
function setFileSize(fsize) {
    filesize=fsize;
    $("filesize").innerHTML=fsize;
}

//设置已经下载的,并计算百分比
function setDownloaded(fsize) {
    $("downloaded").innerHTML=fsize;
    if(filesize>0) {
        var percent=Math.round(fsize*100/filesize);
        $("progressbar").style.width=(percent+"%");
        if(percent>0) {
            $("progressbar").innerHTML=percent+"%";
            $("progressText").innerHTML="";
        } else {
            $("progressText").innerHTML=percent+"%";
        }
    }
}
</script>
<?php
//ini_set("display_errors", "On");
//error_reporting(E_ALL | E_STRICT);
//var_dump(ini_get('allow_url_fopen'));die;
set_time_limit(0);
ini_set("max_execution_time", "0");
header('Content-Type:text/html;charset=utf-8');

/*function get_h($url,$format=0)
    {
        $url_info=parse_url($url);
        $port = isset($url_info['port']) ? $url_info['port'] : 80;
        $fp=fsockopen($url_info['host'], $port, $errno, $errstr, 30);

        if($fp)
        {
            $head = "HEAD ".@$url_info['path']."?".@$url_info['query']." HTTP/1.0\r\nHost: ".@$url_info['host']."\r\n\r\n";
            fputs($fp, $head);
            while(!feof($fp))
            {
                if($header=trim(fgets($fp, 1024)))
                {
                    if($format == 1)
                    {
                        $key = array_shift(explode(':',$header));
                        // the first element is the http header type, such as HTTP 200 OK,
                        // it doesn't have a separate name, so we have to check for it.
                        if($key == $header)
                        {
                            $headers[] = $header;
                        }
                        else
                        {
                            $headers[$key]=substr($header,strlen($key)+2);
                        }
                        unset($key);
                    }
                    else
                    {
                        $headers[] = $header;
                    }
                }
            }
            return $headers;
        }
        else
        {
            return false;
        }
    }
    */



//$url="http://www.yingyong.so/DownLoad.aspx?id=10167&tid=1&file=Sand_Draw_v1_82.apk";//要下载的文件
$url=$_REQUEST['url'];

$headers = get_headers($url,1);


if($headers['Location']) {
    $url = $headers['Location'];
    //获取文件大小
    $filesize = -1;
    $headers = get_headers($url,1);//再次获取

    if ((!array_key_exists("Content-Length", $headers))) $filesize=0;
    $filesize = $headers["Content-Length"];

}else{
    //获取文件大小
    $filesize = -1;
    if ((!array_key_exists("Content-Length", $headers))) $filesize=0;
    $filesize = $headers["Content-Length"];
}

$target = '/mnt/html/xiaobian_mumayi/uploads/soft/xiaobian/largefiles/'.pathinfo($url,PATHINFO_BASENAME);


//$newfname="test.apk";//本地存放位置
$file = @fopen ($url, "rb");

if ($file) {
    //文件返回大小计算进度
    if ($filesize != -1) echo "<script>setFileSize($filesize);</script>";//在前台显示文件大小
    $newf = fopen ($target, "wb");
    $downlen=0;
    if ($newf) {
        while(!feof($file)) {
            $data=fread($file, 1024 * 8 );//默认获取8K
            $downlen+=strlen($data);//累计已经下载的字节数
            fwrite($newf, $data, 1024 * 8 );
            echo "<script>setDownloaded($downlen);</script>";//在前台显示已经下载文件大小
            ob_flush();
            flush();
        }
        echo $target;
    }
    if ($file) fclose($file);
    if ($newf) fclose($newf);
}
?>
</body>
</html>